#!/usr/bin/env python3
import http.client
import json



def create_pr(owner, repo, access_token, data):
    '''
    创建Pull Request
    https://docs.gitcode.com/docs/apis/post-api-v-5-repos-owner-repo-pulls
    '''
    conn = http.client.HTTPSConnection("api.gitcode.com")

    payload = json.dumps({
    "title": data['title'],
    "head": data['head'],
    "base": data['base'],
    "body": data['body'],
    "milestone_number": data['milestone_number'],
    "labels": data['labels'],
    "issue": data['issue'],
    "assignees": data['assignees'],
    "testers": data['testers'],
    "prune_source_branch": data['prune_source_branch'],
    "draft": data['draft'],
    "squash": data['squash'],
    "squash_commit_message": data['squash_commit_message'],
    "fork_path": data['fork_path']
    })
    headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
    }

    conn.request("POST", f"/api/v5/repos/{owner}/{repo}/pulls?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")


def merge_pr(owner, repo, number, access_token):
    '''
    合并Pull Request
    https://docs.gitcode.com/docs/apis/put-api-v-5-repos-owner-repo-pulls-number-merge
    '''

    conn = http.client.HTTPSConnection("api.gitcode.com")
    payload = json.dumps({
    "merge_method": "string"
    })
    headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
    }
    conn.request("PUT", f"/api/v5/repos/{owner}/{repo}/pulls/{number}/merge?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")


def create_pr_comment(owner, repo, number, access_token, data):
    '''
    提交pull request评论
    https://docs.gitcode.com/docs/apis/post-api-v-5-repos-owner-repo-pulls-number-comments
    '''
    conn = http.client.HTTPSConnection("api.gitcode.com")

    payload = json.dumps({
    "body": data['body'],
    "path": data['path'],
    "position": data['position'],
    "need_to_resolve": data['need_to_resolve']
    })
    headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
    }

    conn.request("POST", f"/api/v5/repos/{owner}/{repo}/pulls/{number}/comments?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")

def get_pr_file_list(owner, repo, number, access_token):
    '''
    Pull RequestCommit文件列表
    https://docs.gitcode.com/docs/apis/get-api-v-5-repos-owner-repo-pulls-number-files
    '''

    conn = http.client.HTTPSConnection("api.gitcode.com")
    payload = ''
    headers = {
    'Accept': 'application/json'
    }
    conn.request("GET", f"/api/v5/repos/{owner}/{repo}/pulls/{number}/files?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")

def get_single_pr(owner, repo, number, access_token):
    '''
    获取单个Pull Request
    https://docs.gitcode.com/docs/apis/get-api-v-5-repos-owner-repo-pulls-number
    '''

    conn = http.client.HTTPSConnection("api.gitcode.com")
    payload = ''
    headers = {
    'Accept': 'application/json'
    }
    conn.request("GET", f"/api/v5/repos/{owner}/{repo}/pulls/{number}?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")


def create_pr_label(owner, repo, number, access_token, label):
    '''
    创建 Pull Request标签
    https://docs.gitcode.com/docs/apis/post-api-v-5-repos-owner-repo-pulls-number-labels
    '''
    conn = http.client.HTTPSConnection("api.gitcode.com")

    payload = json.dumps([
    label
    ])
    headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
    }

    conn.request("POST", f"/api/v5/repos/{owner}/{repo}/pulls/{number}/labels?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")

def get_pr_labels(owner, repo, number, access_token):
    '''
    获取某个 Pull Request的所有标签
    https://docs.gitcode.com/docs/apis/get-api-v-5-repos-owner-repo-pulls-number-labels
    '''

    conn = http.client.HTTPSConnection("api.gitcode.com")
    payload = ''
    headers = {
    'Accept': 'application/json'
    }
    conn.request("GET", f"/api/v5/repos/{owner}/{repo}/pulls/{number}/labels?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")


def review_pr(owner, repo, number, access_token, force):
    '''
    处理 Pull Request审查
    https://docs.gitcode.com/docs/apis/post-api-v-5-repos-owner-repo-pulls-number-review
    '''

    conn = http.client.HTTPSConnection("api.gitcode.com")
    payload = json.dumps({
    "force": force
    })
    headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
    }
    conn.request("POST", f"/api/v5/repos/{owner}/{repo}/pulls/{number}/review?access_token={access_token}", payload, headers)
    res = conn.getresponse()
    data = res.read()
    #print(data.decode("utf-8"))
    return data.decode("utf-8")



if __name__ == "__main__":
    print("测试:")
